//
//  GDUserAccountModel.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/3.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDUserAccountModel.h"

#define GDUserName @"userName"
#define GDUserPwd @"userPwd"
#define GDUserRegister @"userIsRegister"


@implementation GDUserAccountModel

#pragma mark 带有参数的实例化对象
+(void)initWithAccount:(NSString *)u_name withPwd:(NSString *)u_pwd withIcon:(NSData *)iconData{
    GDUserAccountModel * model = [[GDUserAccountModel alloc] init];
    model.userName = u_name;
    model.userPwd = u_pwd;
    model.userIconData = iconData;
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:u_name forKey:GDUserName];
    [userDefault setObject:u_pwd forKey:GDUserPwd];
    [userDefault setBool:YES forKey:GDUserRegister];
    [userDefault synchronize];
//    NSString * iconFile = [GDCachesFilePath stringByAppendingString:[NSString stringWithFormat:@"/%@",u_name]];
//    [model.userIconData writeToFile:iconFile atomically:YES];
}

#pragma mark 不带参数的实例化对象，返回具有数据的对象
+(instancetype)shareInstance{
    return  [[self alloc ] init];
}

+(BOOL)updatePwd:(NSString *)pwd{

    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:pwd forKey:GDUserPwd];
    [userDefault synchronize];
    return  true;
    
}

#pragma mark 分配空间内存
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static GDUserAccountModel * accountModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        accountModel = [super allocWithZone:zone];
        NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
        accountModel.userName = [userDefault objectForKey:GDUserName];
        accountModel.userPwd = [userDefault objectForKey:GDUserPwd];
        accountModel.isRegister = [[userDefault objectForKey:GDUserRegister] intValue] == 1? true:false;
//        accountModel.userIconData = [NSString str];
 
    });
    return accountModel;
}
@end
